package com.zgjkhis.utils;

import com.zgjkhis.domain.PageResult;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页工具
 * @author yxl
 * @date 2018-12-10
 */
public class PageUtil extends cn.hutool.core.util.PageUtil {

    /**
     * List 分页
     */
    public static <T> List<T> toPage(int page, int size , List<T> list) {
        int fromIndex = page * size;
        int toIndex = page * size + size;
        if(fromIndex > list.size()){
            return new ArrayList();
        } else if(toIndex >= list.size()) {
            return list.subList(fromIndex,list.size());
        } else {
            return list.subList(fromIndex,toIndex);
        }
    }

    /**
     * 自定义分页
     */
    public static <T> PageResult<T> toPage(List<T> object, long totalElements) {
        return new PageResult<>(totalElements, object);
    }

}
